﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using Freedback.Core.Context;
using Freedback.Core.Repositories;

namespace FreedBack.Application.Modules
{
    public class DataProviderModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder
                .RegisterType<FreedbackContext>()
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();

            var repositoryMapperTypes = typeof(IRepository).Assembly.GetExportedTypes()
                .Where(t => !t.IsInterface && !t.IsAbstract)
                .Where(t => typeof(IRepository).IsAssignableFrom(t))
                .ToArray();
            foreach (var type in repositoryMapperTypes)
            {
                if (type.IsGenericType)
                    builder.RegisterGeneric(type).AsImplementedInterfaces().InstancePerLifetimeScope();
                else
                    builder.RegisterType(type).AsImplementedInterfaces().InstancePerLifetimeScope();
            }
        }
    } 
}